export class AMapPoiSearch {
  constructor(map) {
    this.map = map;
  }
  // 初始化poi插件
  init(inputId, autocompleteOption, placeSearchOption) {
    return new Promise(async (resolve) => {
      AMap.plugin(['AMap.AutoComplete', 'AMap.PlaceSearch'], () => {
        // 实例化Autocomplete:根据输入关键字提示匹配信息
        const autoOption = Object.assign({}, autocompleteOption, {
          // input 为绑定输入提示功能的input的DOM ID
          input: inputId,
        });
        /* 不给 AMap.PlaceSearch 提供 map 实例, AMap.PlaceSearch 就不会自动渲染 marker */
        const searchOption = Object.assign({}, placeSearchOption);
        this.autoComplete = new AMap.AutoComplete(autoOption);
        this.placeSearch = new AMap.PlaceSearch(Object.assign(searchOption, { map: this.map }));
        this.bindEvent();
        resolve();
      });
    });
  }
  bindEvent() {
    this.autoComplete.on('select', (e) => {
      // placeSearch绑定地图，使用自带扎点
      this.placeSearch.search(e.poi.name, (status, e) => {});
    });
  }

  clear() {
    if (this.placeSearch) {
      this.placeSearch.clear();
    }
  }
  onSearch(res) {
    /* 这里是对外输出结果的接口 */
  }
}
